import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_config.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_09_game/base/widget/base_get_builder.dart';
import 'package:flutter_09_game/widgets/appbar/common_app_bar.dart';
import 'package:flutter_09_game/widgets/button/common_button.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'home_download_center_logic.dart';
import 'models/exchange_item_model.dart';

class HomeDownloadCenterPage extends StatelessWidget {
  HomeDownloadCenterPage({super.key});

  final logic = Get.put(HomeDownloadCenterLogic());

  @override
  Widget build(BuildContext context) {
    return BaseGetBuilder<HomeDownloadCenterLogic>(
      appBar: CommonAppBar(
        title: LocaleKeys.download.tr,
        actions: [
          GestureDetector(
            onTap: () {
              AppUserManager().showServiceSheet();
            },
            child: AutoImage(
              Assets.commonServiceBtn,
              width: 32.w,
            ),
          ),
          10.horizontalSpace,
        ],
      ),
      backgroundColor: AppColor.pageBgColor,
      builder: (c) {
        return SingleChildScrollView(
          padding: EdgeInsets.all(16.w),
          child: Column(
            children: [
              _buildHeader(),
              16.verticalSpaceFromWidth,
              _buildExchangeList(),
            ],
          ),
        );
      },
    );
  }

  /// 构建头部说明区域
  Widget _buildHeader() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: AppColor.navBgColor,
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            LocaleKeys.buyUsdtTrxTip.tr,
            style: TextStyle(
              fontSize: 12.sp,
              color: AppColor.textPrimary.withOpacity(0.7),
              height: 1.5,
            ),
          ),
          8.verticalSpaceFromWidth,
          Text(
            LocaleKeys.vpnTip.tr,
            style: TextStyle(
              fontSize: 12.sp,
              color: AppColor.primary,
              fontWeight: FontWeight.w600,
              height: 1.5,
            ),
          ),
        ],
      ),
    );
  }

  /// 构建交易所列表
  Widget _buildExchangeList() {
    return Column(
      children:
          logic.exchangeList.map((item) => _buildExchangeItem(item)).toList(),
    );
  }

  /// 构建单个交易所项目
  Widget _buildExchangeItem(ExchangeItemModel item) {
    return Container(
      margin: EdgeInsets.only(bottom: 16.w),
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: AppColor.navBgColor,
        borderRadius: BorderRadius.circular(12.w),
      ),
      child: Row(
        children: [
          // 交易所logo
          Container(
            width: 44.w,
            height: 44.w,
            decoration: BoxDecoration(
              color: AppColor.navBgColor,
              borderRadius: BorderRadius.circular(8.w),
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(8.w),
              child: AutoImage(
                item.logo,
                width: 32.w,
                height: 32.w,
                fit: BoxFit.cover,
              ),
            ),
          ),

          4.horizontalSpace,

          // 交易所域名
          Expanded(
            child: Text(
              item.domain,
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColor.textPrimary.withOpacity(0.94),
                fontWeight: FontWeight.w500,
              ),
            ),
          ),

          // 按钮组
          Row(
            children: [
              // 教程按钮
              CommonButton(
                title: LocaleKeys.tutorial.tr,
                width: 72.w,
                height: 32.w,
                fontSize: 12.sp,
                enable: false,
                alwaysCanClick: true,
                onClick: () => logic.openTutorial(item.tutorialUrl),
              ),



              8.horizontalSpace,

              // 下载按钮
              CommonButton(
                title: LocaleKeys.download.tr,
                width: 72.w,
                height: 32.w,
                fontSize: 12.sp,

                // backgroundColors: [AppColor.primary, AppColor.primary],
                // textColor: AppColor.textPrimary.withOpacity(0.94),
                // borderRadius: BorderRadius.circular(8.w),
                onClick: () => logic.openDownload(item.downloadUrl),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
